package com.qf.sportsevent.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by Administrator on 2016/12/17.
 */

public class FormatUtils {
    public final static String[] MONTHS=new String[]{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
    public final static String[] NUMBERS=new String[]{"零","一","二","三","四","五","六","七","八","九","十"};

    public static String formatNumberToChinese(int num){
        if(num>0 && num<NUMBERS.length) {
            return NUMBERS[num];
        }else{
            return null;
        }
    }

    public static String formatMonthDayTime(String dateStr){
        if(dateStr!=null) {
            String[] strings = dateStr.split("\\s");
            for (int i = 0; i < MONTHS.length; i++) {
                if (strings[1].equals(MONTHS[i])) {
                    return (i + 1) + "-" + strings[2] + "\t" + strings[3].substring(0,strings[3].lastIndexOf(":"));
                }
            }
        }
        return null;
    }

    public static String checkDate(String dateStr){
        Date todayDate=new Date();

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(todayDate);
        calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动
        Date tomorrowDate = calendar.getTime(); //这个时间就是日期往后推一天的结果

        boolean isToday = checkDateDetail(todayDate, dateStr);
        boolean isTomorrow = checkDateDetail(tomorrowDate, dateStr);
        String[] dayAndTime = formatMonthDayTime(dateStr).split("\\t");
        String dayStr = isToday ? "今天" : isTomorrow ? "明天" : dayAndTime[0];
        return dayStr+"\t\t"+dayAndTime[1];
    }


    public static boolean checkDateDetail(Date date,String dateStr){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        String[] dateStrings = dateString.split("-");
        String[] contestStrings = dateStr.split("\\s");
        int month=0;
        for (int i = 0; i < MONTHS.length; i++) {
            if (contestStrings[1].equals(MONTHS[i])) {
                month=i+1;
            }
        }
        if(month!=0&&month==Integer.parseInt(dateStrings[1])&&contestStrings[2].equals(dateStrings[2])){
            return true;
        }else{
            return false;
        }
    }
}
